package com.lwj.securitydemo.web.config;

import com.lwj.securitydemo.web.filter.TimeFilter;
import com.lwj.securitydemo.web.interceptor.TimeInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;
import java.util.List;

@Configuration
public class WebConfig implements WebMvcConfigurer {

    /**
     * 注入自定义的TimeInterceptor
     */
    @Autowired
    private TimeInterceptor timeInterceptor;

    /**
     * 配置自定义的Interceptor
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(timeInterceptor);
    }

    /**
     * 配置过滤器
     * @return
     */
    @Bean
    public FilterRegistrationBean timeFilter() {
        FilterRegistrationBean registrationBean = new FilterRegistrationBean();
        //  new一個自定義的filter出來
        TimeFilter timeFilter = new TimeFilter();
        //  將自定義的filter放入到registrationBean
        registrationBean.setFilter(timeFilter);

        //  設置需要被攔截的親求類型
        List<String> filterUrls = new ArrayList<>();
        filterUrls.add("/*");
        registrationBean.setUrlPatterns(filterUrls);

        return registrationBean;
    }
}
